import { BaseWindow } from "../../../component/window/BaseWindow";
import { ShopItemInfo } from "../../resource/info/ShopItemInfo";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { BaseItem } from "../common/BaseItem";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { NumInput } from "../common/NumInput";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ItemData } from "../../resource/info/ItemData";
import { Cache } from "../../cache/Cache";
import { SGuildShopItem } from "../../../../Message/Game/SGuildShopItem";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { CareerUtil } from "../common/util/CareerUtil";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { ItemConfig } from "../../resource/ItemConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EDrug } from "../../../../Message/Public/EDrug";
import { SMoney } from "../../../../Message/Game/SMoney";
import { SPlayerGuildInfo } from "../../../../Message/Game/SPlayerGuildInfo";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { MsgManager } from "../../manager/MsgManager";
import { TItem } from "../../../../Message/Db/Tables/TItem";
import { GButton } from "../../../../com/mui/controls/GButton";
import { _shopItemInfo } from "../../../../this/_shopItemInfo";
import { _guildShopItemCode } from "../../../../this/_guildShopItemCode";
type int = number;
//class BuyBatchPanel
    
    export  class BuyBatchPanel extends BaseWindow
    {
        constructor(arg1: ShopItemInfo=null)
        {
            super();
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoHandler);
            return;
        }

        public get showCenter(): Boolean
        {
            return this._showCenter;
        }

        public init(): void
        {
            this.setSize(280, 175);
            this.titleHeight = 28;
            this.title = Language.getString(90266);
            ObjCreate.createBg(19, 42, 242, 120, this);
            ObjCreate.createBg(45, 50, 38, 38, this, ImagesConst.PackItemBg);
            this._baseItem = new BaseItem();
            this._baseItem.setSize(32, 32);
            this._baseItem.move(47, 52);
            this.addChild(this._baseItem);
            this._nameTxt = ObjCreate.createTextField("", 105, 53, 220, 20, this);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this.addChild(this._nameTxt);
            this._careerTxt = ObjCreate.createTextField("", 165, 53, 220, 20, this);
            this._careerTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this.addChild(this._careerTxt);
            ObjCreate.createTextField(Language.getString(90267), 105, 75, 220, 20, this);
            this._priceTxt = ObjCreate.createTextField("", 165, 75, 220, 20, this, GlobalStyle.textFormat6);
            this._priceTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._unitBitmap = new Object /* flash.display.Bitmap */();
            this._unitBitmap.x = 195;
            this._unitBitmap.y = 75;
            this.addChild(this._unitBitmap);
            this._unitTxt = ObjCreate.createTextField("", 195, 75, 220, 20, this);
            this._unitTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            ObjCreate.createTextField(Language.getString(90268), 40, 100, 220, 20, this);
            this._numInput = new NumInput();
            this._numInput.minNum = 1;
            this._numInput.setMaxNum(99);
            this._numInput.x = 100;
            this._numInput.y = 100;
            this._numInput.addEventListener(flash.events.Event.CHANGE, this.onNumChangeHandler);
            this.addChild(this._numInput);
            this._buyBtn = ObjCreate.createGButton(Language.getString(90269), 195, 97, 49, 24, this);
            this._buyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            ObjCreate.createTextField(Language.getString(90270), 40, 120, 220, 20, this);
            this._costTxt = ObjCreate.createTextField("", 105, 120, 220, 20, this, GlobalStyle.textFormat6);
            this._costTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._costBitmap = new Object /* flash.display.Bitmap */();
            this._costBitmap.x = 150;
            this._costBitmap.y = 120;
            this.addChild(this._costBitmap);
            this._costUnitTxt = ObjCreate.createTextField("", 150, 120, 220, 20, this);
            this._costUnitTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            ObjCreate.createTextField(Language.getString(90271), 40, 140, 220, 20, this);
            this._propertyTxt = ObjCreate.createTextField("", 105, 140, 220, 20, this, GlobalStyle.textFormat6);
            this._propertyTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._propertyBitmap = new Object /* flash.display.Bitmap */();
            this._propertyBitmap.x = 150;
            this._propertyBitmap.y = 140;
            this.addChild(this._propertyBitmap);
            this._propertyUnitTxt = ObjCreate.createTextField("", 150, 140, 220, 20, this);
            this._propertyUnitTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._tipText = ObjCreate.createTextField(Language.getString(90272), 205, 122, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16711680));
            this._tipText.visible = false;
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            var loc1=0;
            var loc2=null;
            this._guildShopItemCode = -1;
            if (arg1) 
            {
                if (typeof arg1 === "shopiteminfo") 
                {
                    this._shopItemInfo = arg1 as ShopItemInfo;
                    this._itemData = new ItemData(this._shopItemInfo.tshopSell.itemCode);
                    this._itemInfo = this._shopItemInfo.itemInfo.item;
                    this._uint = this._shopItemInfo.unit;
                    this._property = this.getPropertyByUnit(this._uint);
                    loc1 = this._shopItemInfo.tshopSell.activeOffer;
                    if (Cache.instance.shop.isShopMallDiscount && loc1 > 0) 
                    {
                        this._unitPrice = this._shopItemInfo.tshopSell.activeOffer;
                    }
                    else 
                    {
                        this._unitPrice = this._shopItemInfo.tshopSell.price;
                    }
                    if (rest && rest.length > 0) 
                    {
                        this.amount = rest[0];
                        this._numInput.currentNum = this.amount;
                    }
                    else 
                    {
                        this.initAmount();
                    }
                }
                else if (typeof arg1 === "sguildshopitem") 
                {
                    loc2 = arg1 as SGuildShopItem;
                    this._guildShopItemCode = loc2.itemCode;
                    this._itemData = new ItemData(this._guildShopItemCode);
                    this._itemInfo = this._itemData.itemInfo.item;
                    this._uint = -1;
                    this._property = this.getPropertyByUnit(this._uint);
                    this._unitPrice = loc2.price;
                    if (ItemsUtil.isGuildPartyHot(this._itemData)) 
                    {
                        if (loc2.buyLimit >= 5) 
                        {
                            this._numInput.currentNum = 5;
                        }
                        else 
                        {
                            this._numInput.currentNum = loc2.buyLimit;
                        }
                        this._numInput.setMaxNum(loc2.buyLimit);
                    }
                    else 
                    {
                        this._numInput.maxNum = loc2.buyLimit;
                    }
                    this.amount = this._numInput.currentNum;
                }
                this._totalPrice = this._unitPrice * this.amount;
                this._nameTxt.htmlText = ItemsUtil.getItemName(this._itemData);
                this._priceTxt.text = this._unitPrice.toString();
                this._propertyTxt.htmlText = this._property.toString();
                this._baseItem.itemData = this._itemData;
                this._priceImgName = GameDefConfig.instance.getEPrictUnitImg(this._uint);
                this._uintName = this.getUnitName(this._uint);
                if (this._priceImgName == "") 
                {
                    this._unitBitmap.bitmapData = null;
                    this._costBitmap.bitmapData = null;
                    this._propertyBitmap.bitmapData = null;
                    this._unitTxt.htmlText = this._uintName;
                    this._costUnitTxt.htmlText = this._uintName;
                    this._propertyUnitTxt.htmlText = this._uintName;
                }
                else 
                {
                    this._unitBitmapData = GlobalClass.getBitmapData(this._priceImgName);
                    this._unitBitmap.bitmapData = this._unitBitmapData;
                    this._costBitmap.bitmapData = this._unitBitmapData;
                    this._propertyBitmap.bitmapData = this._unitBitmapData;
                    this._unitTxt.text = "";
                    this._costUnitTxt.text = "";
                    this._propertyUnitTxt.text = "";
                }
                this._itemCareer = this._itemInfo.career;
                if (this._itemCareer == 0) 
                {
                    this._careerName = "";
                }
                else 
                {
                    this._careerName = CareerUtil.getEquipCareerName(this._itemCareer);
                    if (CareerUtil.isCareerSuit(this._itemCareer, Cache.instance.role.roleInfo.career)) 
                    {
                        this._careerName = HTMLUtil.addColor("（" + this._careerName + "）", "#f0ea3f");
                    }
                    else 
                    {
                        this._careerName = HTMLUtil.addColor("（" + this._careerName + "）", "#ff0000");
                    }
                }
                this._careerTxt.htmlText = this._careerName;
                this._careerTxt.x = this._nameTxt.x + this._nameTxt.textWidth + 10;
                this.checkMoney();
            }
            return;
        }

        public setMaxNum(arg1: int): void
        {
            this._maxNum = arg1;
            this._numInput.setMaxNum(arg1);
            return;
        }

        protected initAmount(): void
        {
            var loc2=0;
            var loc1=ItemConfig.instance.getInfoByCode(this._uint);
            if (loc1) 
            {
                this.amount = int(this._property / this._unitPrice);
                this._numInput.setMaxNum(this.amount);
            }
            else if (this._itemInfo && ItemsUtil.isOverlay(this._itemInfo)) 
            {
                if (ItemsUtil.isHook(this._itemData)) 
                {
                    this.amount = 20;
                }
                else if (ItemsUtil.isPropColorStone(this._itemData) || ItemsUtil.isPropTransportFlushToken(this._itemData)) 
                {
                    this.amount = 10;
                }
                else if (this._itemData.category == ECategory._ECategoryDrug && !(this._itemData.type == EDrug._EDrugLifePetBag) && !(this._itemData.type == EDrug._EDrugLifeBag) && !(this._itemData.type == EDrug._EDrugManaBag)) 
                {
                    this.amount = 999;
                }
                else if (ItemsUtil.isIllustrate(this._itemData) || ItemsUtil.isSpecialPetSkillBook(this._itemData)) 
                {
                    this.amount = 1;
                }
                else 
                {
                    loc2 = int(this._property / this._unitPrice);
                    if (loc2 != 0) 
                    {
                        if (loc2 < 10) 
                        {
                            this.amount = loc2;
                        }
                        else 
                        {
                            this.amount = 10;
                        }
                    }
                    else 
                    {
                        this.amount = 1;
                    }
                }
            }
            else 
            {
                this.amount = 1;
            }
            if (this._maxNum == -1 && !loc1) 
            {
                if (this.amount != 999) 
                {
                    this._numInput.setMaxNum(99);
                }
                else 
                {
                    this._numInput.setMaxNum(999);
                }
            }
            this._numInput.currentNum = this.amount;
            return;
        }

        protected checkMoney(): void
        {
            var loc1=null;
            if (this._property >= this._totalPrice) 
            {
                loc1 = "#f0ea3f";
                this._tipText.visible = false;
            }
            else 
            {
                this._tipText.visible = true;
                loc1 = "#ff0000";
            }
            this._costTxt.htmlText = HTMLUtil.addColor(this._totalPrice.toString(), loc1);
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            this._property = this.getPropertyByUnit(this._uint);
            this._propertyTxt.text = this._property.toString();
            this.checkMoney();
            return;
        }

        /* internal  */onGuildUpdateInfoHandler(arg1: SPlayerGuildInfo): void
        {
            this.onMoneyUpdateHandler();
            return;
        }

        public set showCenter(arg1: Boolean)
        {
            this._showCenter = arg1;
            if (arg1) 
            {
                this._isFirstCenter = this._showCenter;
            }
            this._isCenter = this._showCenter;
            return;
        }

        protected onBuyBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._property < this._totalPrice) 
            {
                if (this._uint == EPrictUnit._EPriceUnitCoin || this._uint == EPrictUnit._EPriceUnitCoinBind) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, this._totalPrice - this._property));
                }
                MsgManager.showRollTipsMsg(Language.getStringByParam(90273, this._uintName));
                return;
            }
            if (this._guildShopItemCode == -1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopBuyItem, {"itemInfo": _shopItemInfo, "amount": this.amount}));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildBuyShopItem, {"itemCode": _guildShopItemCode, "amount": this.amount}));
            }
            hide();
            return;
        }

        /* internal  */onNumChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.amount = this._numInput.currentNum;
            this._totalPrice = this._unitPrice * this.amount;
            this._costTxt.text = this._totalPrice.toString();
            this.checkMoney();
            return;
        }

        protected getPropertyByUnit(arg1: int): number
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc1=0;
            var loc2=Cache.instance.role.money;
            if (arg1 != EPrictUnit._EPriceUnitCoinBind) 
            {
                if (arg1 != EPrictUnit._EPriceUnitCoin) 
                {
                    if (arg1 != EPrictUnit._EPriceUnitGold) 
                    {
                        if (arg1 != -1) 
                        {
                            if (arg1 != EPrictUnit._EPriceUnitHonour) 
                            {
                                if (arg1 != EPrictUnit._EPriceUnitArena) 
                                {
                                    if (arg1 != EPrictUnit._EPriceUnitInterHonour) 
                                    {
                                        if (arg1 != EPrictUnit._EPriceUnitPetCoin) 
                                        {
                                            if (arg1 != EPrictUnit._EPriceUnitMagicStone) 
                                            {
                                                if (arg1 != EPrictUnit._EPriceUnitCountryContribution) 
                                                {
                                                    if (arg1 != EPrictUnit._EPriceUnitRobFlagHonour) 
                                                    {
                                                        if (arg1 != EPrictUnit._EPriceUnitRobIslandCoin) 
                                                        {
                                                            if (arg1 != EPrictUnit._EPriceUnitSpiritCoin) 
                                                            {
                                                                if (arg1 != EPrictUnit._EPriceUnitLadder) 
                                                                {
                                                                    if (arg1 != EPrictUnit._EPriceUnitArenaCrossCoin) 
                                                                    {
                                                                        if (arg1 != EPrictUnit._EPriceUnitDominion) 
                                                                        {
                                                                            if (arg1 > 0) 
                                                                            {
                                                                                loc4 = [];
                                                                                if (loc5 = ItemConfig.instance.getInfoByCode(arg1)) 
                                                                                {
                                                                                    loc4.push(arg1);
                                                                                    loc4.push(loc5.item.codeUnbind);
                                                                                    loc1 = Cache.instance.pack.backPackCache.getItemCountByCodeArray(loc4);
                                                                                }
                                                                            }
                                                                        }
                                                                        else 
                                                                        {
                                                                            loc1 = loc2.dominionCoin;
                                                                        }
                                                                    }
                                                                    else 
                                                                    {
                                                                        loc1 = loc2.arenaCrossCoin;
                                                                    }
                                                                }
                                                                else 
                                                                {
                                                                    loc1 = loc2.ladderCoin;
                                                                }
                                                            }
                                                            else 
                                                            {
                                                                loc1 = loc2.spiritCoin;
                                                            }
                                                        }
                                                        else 
                                                        {
                                                            loc1 = loc2.robIslandCoin;
                                                        }
                                                    }
                                                    else 
                                                    {
                                                        loc1 = loc2.robFlagHonour;
                                                    }
                                                }
                                                else 
                                                {
                                                    loc1 = loc2.countryContribution;
                                                }
                                            }
                                            else 
                                            {
                                                loc1 = loc2.magicStone;
                                            }
                                        }
                                        else 
                                        {
                                            loc1 = loc2.petCoin;
                                        }
                                    }
                                    else 
                                    {
                                        loc1 = loc2.interHonour;
                                    }
                                }
                                else 
                                {
                                    loc1 = loc2.arena;
                                }
                            }
                            else 
                            {
                                loc1 = loc2.honour;
                            }
                        }
                        else if (loc3 = Cache.instance.guild.myGuildInfo) 
                        {
                            loc1 = loc3.contribution;
                        }
                    }
                    else 
                    {
                        loc1 = loc2.gold;
                    }
                }
                else 
                {
                    loc1 = loc2.coin;
                }
            }
            else 
            {
                loc1 = loc2.coinBind + loc2.coin;
            }
            return loc1;
        }

        protected getUnitName(arg1: int): string
        {
            var loc1=null;
            var loc2=null;
            if (arg1 != -1) 
            {
                loc1 = GameDefConfig.instance.getEPrictUnitName(this._uint);
            }
            else 
            {
                loc1 = Language.getString(90274);
            }
            if (loc1 == null || loc1 == "") 
            {
                if (arg1 > 0) 
                {
                    loc2 = ItemConfig.instance.getInfoByCode(arg1);
                    if (loc2) 
                    {
                        loc1 = loc2.htmlName;
                    }
                }
            }
            return loc1;
        }

        protected /* var */_propertyUnitTxt: Object /* flash.text.TextField */;

        protected /* var */_tipText: Object /* flash.text.TextField */;

        public /* var */amount: int=1;

        protected /* var */_unitBitmapData: Object /* flash.display.BitmapData */;

        protected /* var */_totalPrice: number=0;

        protected /* var */_itemInfo: TItem;

        protected /* var */_unitPrice: int;

        protected /* var */_showCenter: Boolean=false;

        protected /* var */_unitBitmap: Object /* flash.display.Bitmap */;

        protected /* var */_uint: int;

        protected /* var */_uintName: string="";

        protected /* var */_itemData: ItemData;

        protected /* var */_property: number=0;

        protected /* var */_careerName: string="";

        protected /* var */_itemCareer: int;

        /* internal */ /* var */_guildShopItemCode: int=-1;

        /* internal */ /* var */_shopItemInfo: ShopItemInfo;

        /* internal */ /* var */_maxNum: int=-1;

        protected /* var */_baseItem: BaseItem;

        protected /* var */_nameTxt: Object /* flash.text.TextField */;

        protected /* var */_careerTxt: Object /* flash.text.TextField */;

        protected /* var */_priceTxt: Object /* flash.text.TextField */;

        protected /* var */_unitTxt: Object /* flash.text.TextField */;

        protected /* var */_numInput: NumInput;

        /* internal */ /* var */_buyBtn: GButton;

        protected /* var */_priceImgName: string;

        protected /* var */_costTxt: Object /* flash.text.TextField */;

        protected /* var */_costBitmap: Object /* flash.display.Bitmap */;

        protected /* var */_costUnitTxt: Object /* flash.text.TextField */;

        protected /* var */_propertyTxt: Object /* flash.text.TextField */;

        protected /* var */_propertyBitmap: Object /* flash.display.Bitmap */;
    }
